<?php

namespace common\models\views;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "view_sku_spec".
 *
 * @property int $id
 * @property int $product_id 货品ID
 * @property int $goods_id 商品ID
 * @property int $spec_value_id 规格值ID
 * @property string $spec_value 规格值
 * @property int $spec_id 规格ID
 * @property string $spec_name 规格名称
 * @property int $spec_screen 是否筛选项
 */
class ViewSkuSpec extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return 'view_sku_spec';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['id', 'product_id', 'goods_id', 'spec_value_id', 'spec_id', 'spec_screen'], 'integer'],
            [['spec_value', 'spec_name'], 'required'],
            [['spec_value'], 'string', 'max' => 50],
            [['spec_name'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'product_id' => '货品ID',
            'goods_id' => '商品ID',
            'spec_value_id' => '规格值ID',
            'spec_value' => '规格值',
            'spec_id' => '规格ID',
            'spec_name' => '规格名称',
            'spec_screen' => '是否筛选项',
        ];
    }

    public function getSku() {
        return $this->hasOne(\common\models\shop\ShopGoods::className(), ['id' => 'goods_id']);
    }

    public function getImgUrl() {
        if ($this->sku) {
            return Url::attach($this->sku->cover);
        }
        return '';
    }

    public function fields2() {
        return [
            'id' => 'spec_value_id',
            'k_id' => 'spec_id',
            'k' => 'spec_name',
            'k_s' => function() {
                return 's' . $this->spec_id;
            },
            'name' => 'spec_value',
            'imgUrl'
        ];
    }

}
